package com.lefevre.cms.web.action.feedback;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.feedback.Feedback;
import com.lefevre.cms.service.feedback.FeedbackService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.utils.Verification;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 在线反馈
 *
 * @author Lefevre
 */
@Controller
@Api(tags = "在线反馈")
public class FeedbackController {

    @Resource
    FeedbackService feedbackService;
    @Resource
    SettingService settingService;

    @ResponseBody
    @RequestMapping("/control/feedback/list")
    public String execute(PageForm pageForm, String start_createDate, String end_createDate) throws Exception {

        Map<String, String> error = new HashMap<>();
        Date _start_createDate = null;
        Date _end_createDate = null;
        if (start_createDate != null && !"".equals(start_createDate.trim())) {
            boolean start_createDateVerification = Verification.isTime_minute(start_createDate.trim());
            if (start_createDateVerification) {
                DateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                _start_createDate = dd.parse(start_createDate.trim());
            } else {
                error.put("start_createDate", "请填写正确的日期");
            }
        }
        if (end_createDate != null && !"".equals(end_createDate.trim())) {
            boolean end_createDateVerification = Verification.isTime_minute(end_createDate.trim());
            if (end_createDateVerification) {
                DateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                _end_createDate = dd.parse(end_createDate.trim());
            } else {
                error.put("end_createDate", "请填写正确的日期");
            }
        }
        //比较时间
        Calendar start = Calendar.getInstance();//时间 起始  
        Calendar end = Calendar.getInstance();//时间 结束
        if (_start_createDate != null) {
            start.setTime(_start_createDate);
        }
        if (_end_createDate != null) {
            end.setTime(_end_createDate);
        }
        if (_start_createDate != null && _end_createDate != null) {
            //起始时间与结束时间比较
            int result = start.compareTo(end);
            //起始时间比结束时间大
            if (result > 0) {
                error.put("start_createDate", "起始时间不能比结束时间大");
            }
        }

        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        //起始时间
        if (_start_createDate != null) {
            wrapper.ge(Feedback::getCreateDate, _start_createDate);
        }
        //结束时间
        if (_end_createDate != null) {
            wrapper.le(Feedback::getCreateDate, _end_createDate);
        }
        wrapper.orderByDesc(Feedback::getId);
        PageView<Feedback> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber()
                , pageForm.getPage(), 10);
        int firstindex = (pageForm.getPage() - 1) * pageView.getMaxresult();

        Page<Feedback> feedbackPage = feedbackService.page(new Page<>(firstindex, pageView.getMaxresult()), wrapper);

        QueryResult<Feedback> qr = new QueryResult<>();
        qr.setResultlist(feedbackPage.getRecords());
        qr.setTotalrecord(feedbackPage.getTotal());

        pageView.setQueryResult(qr);
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, pageView));
        }
    }
}
